package leetcode.editor.others;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class 后继者 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        TreeNode(int x, TreeNode left, TreeNode right) {
            this.val = x;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
//        List<Integer> res = new ArrayList<>();
        ti();
////                        1
////                2               3
////        4            5     6          7
//
//        TreeNode root = new TreeNode(1,
//                new TreeNode(2,
//                        new TreeNode(4),
//                        new TreeNode(5)),
//                new TreeNode(3,
//                        new TreeNode(6),
//                        new TreeNode(7)));
////        zhongxubianli_digui(root);
////        zhongxubianli(root);
////        xianxubianli_digui(root);
////        xianxubianli(root);
////        xianxubianli_2(root);
////        houxubianli_digui(root);
//        houxubianli(root);
    }

    private static void xianxubianli_2(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        stack.push(root);
        while (!stack.isEmpty()) {
            cur = stack.pop();
            System.out.print(cur.val + " ");
            if(cur.right != null ) stack.push(cur.right);
            if(cur.left != null ) stack.push(cur.left);
        }
    }

    private static void houxubianli(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        TreeNode pre = null;
        while (!stack.isEmpty() || cur != null) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            if (cur.right == null || cur.right == pre) {
                System.out.print(cur.val + " ");
                pre = cur;
                cur = null;
            } else {
                stack.push(cur);
                cur = cur.right;
            }
        }
    }

    private static void houxubianli_digui(TreeNode root) {
        if (root == null) return;

        houxubianli_digui(root.left);
        houxubianli_digui(root.right);
        System.out.print(root.val + " ");
    }

    private static void xianxubianli(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        while (!stack.isEmpty() || cur != null) {
            while (cur != null) {
                System.out.print(cur.val + " ");
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            cur = cur.right;
        }
    }

    private static void xianxubianli_digui(TreeNode root) {
        if (root == null) return;

        System.out.print(root.val + " ");
        xianxubianli_digui(root.left);
        xianxubianli_digui(root.right);
    }

    private static void zhongxubianli(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        while (!stack.isEmpty() || cur != null) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            System.out.print(cur.val + " ");
            cur = cur.right;
        }
    }

    private static void zhongxubianli_digui(TreeNode root) {
        if (root == null) return;

        zhongxubianli_digui(root.left);
        System.out.print(root.val + " ");
        zhongxubianli_digui(root.right);
    }

    private static void ti() {
        Solution solution = new 后继者().new Solution();
        TreeNode root = new TreeNode(2, new TreeNode(1),new TreeNode(3) );
        TreeNode res = solution.inorderSuccessor(root, root.left);
        if (res == null) {
            System.out.println("null");
        } else {
            System.out.println(res.val);
        }
    }

    class Solution {
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            TreeNode res = null;

            TreeNode cur = root;
            while (cur != null) {
                if (cur.val > p.val) {
                    res = cur;
                    cur = cur.left;
                } else {
                    cur = cur.right;
                }
            }
            return res;
        }


    }

    class Solution1 {
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            Deque<TreeNode> stack = new ArrayDeque<>();
            TreeNode pre = null;
            TreeNode cur = root;
            while (!stack.isEmpty() || cur != null) {
                while (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                }
                cur = stack.pop();

                if (pre == p) {
                    return cur;
                }
                pre = cur;

                cur = cur.right;
            }
            return null;
        }


    }
}
